<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Photos.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Model_DbTable_Photos extends Engine_Db_Table {
	
	protected $_rowClass = 'Sitepage_Model_Photo';
	
	/**
	 * @const SET IMAGE WIDTH
	 */
	const IMAGE_WIDTH = 720;
	/**
	 * @const SET IMAGE HIGHT
	 */
	const IMAGE_HEIGHT = 720;
	/**
	 * @const SET THUMB IMAGE WIDTH
	 */
	const THUMB_WIDTH = 140;
	/**
	 * @const SET THUMB IMAGE HIGHT
	 */
	const THUMB_HEIGHT = 160;
	
	/**
	 * Gets photos of the pages
	 *
	 * @param array params
	 * @return photos for pages
	 */
	public function getPhotos($params = array()) {
		
		$select = $this->select ();
		
		if (isset ( $params ['getSpecialField'] ) && ! empty ( $params ['getSpecialField'] )) {
			$select->from ( $this->info ( 'name' ), array ('count(*) as count', 'title', 'photo_id', 'page_id', 'collection_id', 'file_id' ) );
		} else {
			$select->from ( $this->info ( 'name' ), array ('album_id', 'page_id', 'user_id', 'order', 'title', 'description', 'photo_id', 'view_count', 'comment_count', 'like_count', 'modified_date', 'creation_date', 'photo_hide', 'file_id', 'collection_id' ) );
		}
		
		$select = $select->where ( 'collection_id' . '<> ?', 0 );
		
		if (isset ( $params ['page_id'] ) && ! empty ( $params ['page_id'] )) {
			$select = $select->where ( 'page_id' . '= ?', $params ['page_id'] );
		}
		
		if (isset ( $params ['album_id'] ) && ! empty ( $params ['album_id'] )) {
			$select = $select->where ( 'album_id' . '= ?', $params ['album_id'] );
		}
		
		if (isset ( $params ['user_id'] ) && ! empty ( $params ['user_id'] )) {
			$select = $select->where ( 'user_id' . '<> ?', $params ['user_id'] );
		}
		if (isset ( $params ['photo_hide'] )) {
			$select = $select->where ( 'photo_hide' . '= ?', $params ['photo_hide'] );
		}
		
		if (isset ( $params ['file_id'] ) && ! empty ( $params ['file_id'] )) {
			$select = $select->where ( 'file_id' . '<>?', $params ['file_id'] );
		}
		
		if (isset ( $params ['orderby'] ) && ! empty ( $params ['orderby'] )) {
			$select = $select->order ( $params ['orderby'] );
		}
		
		$select = $select->order ( 'order ASC' );
		if (isset ( $params ['getSpecialField'] ) && ! empty ( $params ['getSpecialField'] )) {
			$select->group ( $this->info ( 'name' ) . '.page_id' );
		}
		if ((isset ( $params ['start'] ) && ! empty ( $params ['start'] )) || (isset ( $params ['end'] ) && ! empty ( $params ['end'] ))) {
			if (! isset ( $params ['end'] )) {
				$params ['end'] = '';
			}
			$select = $select->limit ( $params ['start'], $params ['end'] );
		}
		
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Gets photo id by order
	 *
	 * @param int $album_id
	 * @param int $page_id 
	 * @return photo ids
	 */
	public function getPagePhotosOrder($album_id, $page_id) {
		
		$currentOrder = $this->select ()->from ( $this->info ( 'name' ), 'photo_id' )->where ( 'collection_id = ?', $album_id )->where ( 'page_id = ?', $page_id )->order ( 'order ASC' )->query ()->fetchAll ( Zend_Db::FETCH_COLUMN );
		
		return $currentOrder;
	}
	
	/**
	 * Gets notes data
	 *
	 * @param array $params
	 * @return Zend_Db_Table_Select
	 */
	public function widgetPhotos($params) {
		
		$tablePage = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
		$tablePageName = $tablePage->info ( 'name' );
		$tablePhotoName = $this->info ( 'name' );
		$select = $this->select ()->setIntegrityCheck ( false )->from ( $this->info ( 'name' ) )->joinLeft ( $tablePageName, "$tablePageName.page_id = $tablePhotoName.page_id", array ('page_id', 'title AS page_title', 'closed', 'approved', 'declined', 'draft', 'expiration_date', 'owner_id', 'photo_id as page_photo_id' ) )->where ( 'collection_id <>?', 0 );
		
		if (isset ( $params ['zero_count'] ) && ! empty ( $params ['zero_count'] )) {
			$select = $select->where ( $tablePhotoName . '.' . $params ['zero_count'] . '!= ?', 0 );
		}
		
		if (isset ( $params ['orderby'] ) && ! empty ( $params ['orderby'] )) {
			$select = $select->order ( $tablePhotoName . '.' . $params ['orderby'] );
		}
		if (isset ( $params ['page_id'] ) && ! empty ( $params ['page_id'] )) {
			$select = $select->where ( $tablePhotoName . '.page_id = ?', $params ['page_id'] );
		}
		
		$select = $select->order ( 'photo_id DESC' );
		
		if (isset ( $params ['limit'] ) && ! empty ( $params ['limit'] )) {
			$select = $select->limit ( $params ['limit'] );
		}
		
		$select = $select->where ( $tablePageName . '.search = ?', '1' )->where ( $tablePageName . '.closed = ?', '0' )->where ( $tablePageName . '.approved = ?', '1' )->where ( $tablePageName . '.declined = ?', '0' )->where ( $tablePageName . '.draft = ?', '1' );
		if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
			$select->where ( $tablePageName . '.expiration_date  > ?', date ( "Y-m-d H:i:s" ) );
		}
		return $this->fetchAll ( $select );
	}
	
	/**
	 * Create Photo
	 *
	 * @param array $params
	 * @param array $file
	 */
	public function createPhoto($params, $file) {
		$getPackagePhoto = Engine_Api::_ ()->sitepage ()->getPackageAuthInfo ( 'sitepagealbum' );
		if ($file instanceof Storage_Model_File) {
			$params ['file_id'] = $file->getIdentity ();
		} else {
			//Get image info and resize
			$name = basename ( $file ['tmp_name'] );
			$path = dirname ( $file ['tmp_name'] );
			$extension = ltrim ( strrchr ( $file ['name'], '.' ), '.' );
			
			$mainName = $path . '/m_' . $name . '.' . $extension;
			$thumbName = $path . '/t_' . $name . '.' . $extension;
			
			$image = Engine_Image::factory ();
			$image->open ( $file ['tmp_name'] )->resize ( self::IMAGE_WIDTH, self::IMAGE_HEIGHT )->write ( $mainName )->destroy ();
			
			$image = Engine_Image::factory ();
			$image->open ( $file ['tmp_name'] )->resize ( self::THUMB_WIDTH, self::THUMB_HEIGHT )->write ( $thumbName )->destroy ();
			
			//Store photos
			$photo_params = array ('parent_id' => $params ['page_id'], 'parent_type' => 'sitepage_page' );
			
			$photoFile = Engine_Api::_ ()->storage ()->create ( $mainName, $photo_params );
			$thumbFile = Engine_Api::_ ()->storage ()->create ( $thumbName, $photo_params );
			$photoFile->bridge ( $thumbFile, 'thumb.normal' );
			
			$params ['file_id'] = $photoFile->file_id; //This might be wrong
			$params ['photo_id'] = $photoFile->file_id;
			
			//Remove temp files
			@unlink ( $mainName );
			@unlink ( $thumbName );
		}
		if (! empty ( $getPackagePhoto )) {
			$row = Engine_Api::_ ()->getDbtable ( 'photos', 'sitepage' )->createRow ();
			$row->setFromArray ( $params );
			$row->save ();
			return $row;
		} else {
			return;
		}
	}

}

?>